package com.jsb.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.jsb.service.QuestionService;
import com.jsb.service.UserDealRecordService;
import com.jsb.service.UserService;
import com.jsb.common.config.Const;
import com.jsb.common.utils.StringUtils;
import com.jsb.dto.UserDealRecordComplex;
import com.jsb.model.Question;
import com.jsb.model.User;
import com.jsb.model.UserDealRecord;
import com.jsb.model.UserDealRecordExample;
import com.jsb.model.UserDealRecordExample.Criteria;
import com.jsb.model.UserExample;

@Controller
public class UserDealRecordController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/userDealRecord/list.do";
	
	@Resource
	private UserDealRecordService userDealRecordService;
	@Resource
	private UserService userService;
	@Resource
	private QuestionService questionService;
	
	@RequestMapping("/userDealRecord/list")
	public String list(
			Model model,String goodsType,String payType,String phone,String tradeType,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		UserDealRecordExample userDealRecordExample = new UserDealRecordExample();
		List<User> userByLikePhone = userService.getUserByLikePhone(phone);
		for (User user : userByLikePhone) {
			String userid = user.getPhoneNumber();
			Criteria createCriteria = userDealRecordExample.createCriteria();
			if(StringUtils.isNotBlank(goodsType)){
				createCriteria.andGoodsTypeEqualTo(goodsType);
			}
			//手机号
			if(StringUtils.isNotBlank(userid)){
				createCriteria.andUserIdLike("%"+userid+"%");
			}
			if(StringUtils.isNotBlank(tradeType)){
				createCriteria.andTradeTypeEqualTo(tradeType);
			}
			if(StringUtils.isNotBlank(payType)){
				createCriteria.andPayTypeEqualTo(payType);
			}
			userDealRecordExample.or(createCriteria);
		}
		//类型为个人
//		createCriteria.andAccountTypeEqualTo("2");
		int totalCount = userDealRecordService.countUserDealRecord(userDealRecordExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		userDealRecordExample.setPageIndex((pageNum - 1) * pageSize);
		userDealRecordExample.setPageSize(pageSize);
		userDealRecordExample.setOrderByClause("createDate desc");
		List<UserDealRecord> userDealRecords = userDealRecordService.getPageUserDealRecord(userDealRecordExample);
		List<UserDealRecordComplex> userDealRecordComplexs= new ArrayList<UserDealRecordComplex>();
		for (UserDealRecord userDealRecord : userDealRecords) {
			UserDealRecordComplex userDealRecordComplex= new UserDealRecordComplex();
			userDealRecordComplex.setUserDealRecord(userDealRecord);
			String userId = userDealRecord.getUserId();
			User user = userService.getUserByPhoneNumber(userId);
			userDealRecordComplex.setUser(user);
			userDealRecordComplexs.add(userDealRecordComplex);
		}
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("userDealRecordComplexs", userDealRecordComplexs);
		model.addAttribute("goodsType", goodsType);
		model.addAttribute("payType", payType);
		model.addAttribute("tradeType", tradeType);
		model.addAttribute("phone", phone);
		
		return "/UserDealRecord/list";
	}
	
	@RequestMapping("/userDealRecord/toAdd")
	public String toAdd(Model model) {
		return "/UserDealRecord/add";
	}

	@RequestMapping("/userDealRecord/add")
	public String add(Model model, @ModelAttribute("userDealRecord") UserDealRecord userDealRecord) {
		userDealRecordService.addUserDealRecord(userDealRecord);
		return redirect;
	}

	@RequestMapping("/userDealRecord/del")
	public String del(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		userDealRecordService.delUserDealRecordByUuid(uuid);
		return redirect;
	}

	@RequestMapping("/userDealRecord/toEdit")
	public String toEdit(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		UserDealRecord userDealRecord = userDealRecordService.getUserDealRecordByUuid(uuid);
		model.addAttribute("userDealRecord", userDealRecord);
		return "/UserDealRecord/edit";
	}

	@RequestMapping("/userDealRecord/edit")
	public String edit(Model model, @ModelAttribute("userDealRecord") UserDealRecord userDealRecord) {
		userDealRecordService.updateUserDealRecord(userDealRecord);
		return redirect;
	}

	@RequestMapping("/userDealRecord/view")
	public String view(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		UserDealRecord userDealRecord = userDealRecordService.getUserDealRecordByUuid(uuid);
//		UserDealRecordComplex userDealRecordComplex= new UserDealRecordComplex();
//		userDealRecordComplex.setUserDealRecord(userDealRecord);
		//关联的用户
//		String payId = userDealRecord.getPayId();
//		User sendUser = userService.getUserByPhoneNumber(payId);
//		userDealRecordComplex.setSendUser(sendUser);
		//业务编号
		String businessNo = userDealRecord.getBusinessNo();
		String goodsType = userDealRecord.getGoodsType();
		if(Const.ONE.equals(goodsType)){
			//悬赏
			return "redirect:/question/viewDetail.do?uuid="+businessNo;
		}else if(Const.TWO.equals(goodsType)){
			//偷偷看
			return "redirect:/secretly/viewDetail.do?uuid="+businessNo;
		}else if(Const.THREE.equals(goodsType)){
			//买卡
			return "redirect:/purchaseOrder/viewDetail.do?uuid="+businessNo;
		}else if(Const.FOUR.equals(goodsType)){
			//发红包
			return "redirect:/redEnvelopes/view.do?uuid="+businessNo;
		}
		return "/UserDealRecord/view";
	}
	
	@RequestMapping("/userDealRecord/moneyDetail")
	public String moneyDetail(Model model,
			@RequestParam(value = "uuid", required = true) String uuid,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		
		UserDealRecordExample dealRecordExample= new UserDealRecordExample();
		Criteria criteria = dealRecordExample.createCriteria();
		criteria.andBusinessNoEqualTo(uuid);
		int totalCount = userDealRecordService.countUserDealRecord(dealRecordExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		dealRecordExample.setPageIndex((pageNum - 1) * pageSize);
		dealRecordExample.setPageSize(pageSize);
		
		dealRecordExample.setOrderByClause("createDate desc");
		List<UserDealRecord> userDealRecords = userDealRecordService.selectByExample(dealRecordExample);
		List<UserDealRecordComplex> userDealRecordComplexs= new ArrayList<UserDealRecordComplex>();
		for (UserDealRecord userDealRecord : userDealRecords) {
			UserDealRecordComplex userDealRecordComplex= new UserDealRecordComplex();
			userDealRecordComplex.setUserDealRecord(userDealRecord);
			String userId = userDealRecord.getUserId();
			User user = userService.getUserByPhoneNumber(userId);
			userDealRecordComplex.setUser(user);
			userDealRecordComplexs.add(userDealRecordComplex);
		}
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("userDealRecordComplexs", userDealRecordComplexs);
		return "/UserDealRecord/moneyDetail";
	}
	/**
	 * 平台流水
	 * @param model
	 * @param goodsType
	 * @param payType
	 * @param phone
	 * @param tradeType
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/userDealRecord/platformList")
	public String platformList(
			Model model,String goodsType,String payType,String phone,String tradeType,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		UserDealRecordExample userDealRecordExample = new UserDealRecordExample();
		Criteria createCriteria = userDealRecordExample.createCriteria();
		//悬赏
		if(StringUtils.isNotBlank(goodsType)){
			createCriteria.andGoodsTypeEqualTo(goodsType);
		}
		//手机号
		if(StringUtils.isNotBlank(phone)){
			createCriteria.andUserIdLike("%"+phone+"%");
		}
		//为收入
		if(StringUtils.isNotBlank(tradeType)){
			createCriteria.andTradeTypeEqualTo(tradeType);
		}
		//余额支付
		if(StringUtils.isNotBlank(payType)){
			createCriteria.andPayTypeEqualTo(payType);
		}else{
			payType=null;
		}
		//支付成功
		createCriteria.andStatusEqualTo("0");
		//类型为平台
		createCriteria.andAccountTypeEqualTo("1");
		
		int totalCount = userDealRecordService.countUserDealRecord(userDealRecordExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		userDealRecordExample.setPageIndex((pageNum - 1) * pageSize);
		userDealRecordExample.setPageSize(pageSize);
		userDealRecordExample.setOrderByClause("createDate desc");
		List<UserDealRecord> userDealRecords = userDealRecordService.getPageUserDealRecord(userDealRecordExample);
		List<UserDealRecordComplex> userDealRecordComplexs= new ArrayList<UserDealRecordComplex>();
		for (UserDealRecord userDealRecord : userDealRecords) {
			UserDealRecordComplex userDealRecordComplex= new UserDealRecordComplex();
			userDealRecordComplex.setUserDealRecord(userDealRecord);
			String userId = userDealRecord.getUserId();
			User user = userService.getUserByPhoneNumber(userId);
			userDealRecordComplex.setUser(user);
			userDealRecordComplexs.add(userDealRecordComplex);
		}
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("userDealRecordComplexs", userDealRecordComplexs);
		model.addAttribute("goodsType", goodsType);
		model.addAttribute("payType", payType);
		model.addAttribute("tradeType", tradeType);
		model.addAttribute("phone", phone);
		
		return "/UserDealRecord/platformList";
	}
}
